#include "serial.h"

void serial::ChartInit()
{
    m_chart = new QChart();
    m_chartView = new QChartView(m_chart,this);//画布
    m_chart = m_chartView->chart();//设置展示的图表
    //m_chartView->setRubberBand(QChartView::NoRubberBand);  //矩形缩放
    m_chartView->setRubberBand(QChartView::RectangleRubberBand);  //矩形缩放
    m_chartView->setRenderHint(QPainter::Antialiasing); //设置抗锯齿

    //设置x轴
    axisX = new QValueAxis;
    axisY = new QValueAxis;
    //axisX->setRange(0, 10);         //坐标系区间
    //axisY->setRange(-1, 1);
    axisX->setLabelFormat("%d");    //坐标轴下标的数字类型
    axisY->setLabelFormat("%0.2f");
    axisX->setGridLineVisible(true);//是否显示网格
    axisY->setGridLineVisible(true);
    axisX->setTickCount(10+1);       //设置坐标轴区间格数
    axisY->setTickCount(10+1);
    axisX->setMinorTickCount(5);    //小刻度
    axisX->setTitleText("x");       //x轴名称
    axisY->setTitleText("距离mm");

    //设置图表
    m_chart->addAxis(axisX, Qt::AlignBottom);  //将x坐标轴加到chart上，居下
    m_chart->addAxis(axisY, Qt::AlignLeft);    //将y坐标轴加到chart上，居左
    m_chart->setTitle("超声波距离显示");   //设置图表标题
    m_chart->legend()->show();  //设置图例不可见
    m_chart->legend()->setLayoutDirection(Qt::LeftToRight);
    m_chart->legend()->setAlignment(Qt::AlignLeft);

    //设置曲线
   // series = new QSplineSeries(this);   //平滑曲线的集合
    series = new QLineSeries(this);
    m_chart->addSeries(series);         //将曲线增入chart中
    series->attachAxis(axisX);          //将曲线与轴对应起来
    series->attachAxis(axisY);
    series->setName("1号");
    series->setPointLabelsVisible(true);    //数据点标签可视
    series->setPointsVisible(true);         //数据点可视
    series->setPen(QPen(Qt::yellow,2,Qt::SolidLine)); //设置曲线颜色

    series2 = new QLineSeries(this);
    m_chart->addSeries(series2);         //将曲线增入chart中
    series2->attachAxis(axisX);          //将曲线与轴对应起来
    series2->attachAxis(axisY);
    series2->setName("2号");
    series2->setPen(QPen(Qt::blue,2,Qt::SolidLine));

    //将图表添加进控件
    ui->verticalLayout_2->addWidget(m_chartView);
}

